package com.lyb.spzx.manager.controller;

import com.lyb.common.utils.AuthContextUtil;
import com.lyb.spzx.manager.service.SysMenuService;
import com.lyb.spzx.manager.service.SysUserService;
import com.lyb.spzx.manager.service.ValidateCodeService;
import com.lyb.spzx.model.dto.system.LoginDto;
import com.lyb.spzx.model.dto.system.system.SysUser;
import com.lyb.spzx.model.vo.common.Result;
import com.lyb.spzx.model.vo.common.ResultCodeEnum;
import com.lyb.spzx.model.vo.system.LoginVo;
import com.lyb.spzx.model.vo.system.SysMenuVo;
import com.lyb.spzx.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "用户接口")
@RequestMapping("admin/system/index")
//@CrossOrigin
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
        LoginVo login = sysUserService.login(loginDto);
        return Result.build(login, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取验证码接口")
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> getValidateCode(){
        ValidateCodeVo validateCodeVo = validateCodeService.getValidateCode();
        return Result.build(validateCodeVo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息接口")
    @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token){
        return Result.build(AuthContextUtil.get(),ResultCodeEnum.SUCCESS);
//        SysUser userInfo = sysUserService.getUserInfo(token);
//        return Result.build(userInfo,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出接口")
    @GetMapping("/loginout")
    public Result loginout(@RequestHeader(name = "token") String token){
        sysUserService.loginout(token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/getMenu")
    public Result getMenus(){
        List<SysMenuVo> menuVos = sysMenuService.findUserMenuList();
        return Result.build(menuVos,ResultCodeEnum.SUCCESS);
    }
}
